package com.zhoug.common3.adapters.recycler2;

import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.appbar.AppBarLayout;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.RecyclerViewUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * 滚动冲突工具:解决 swipeRefreshLayout CoordinatorLayout RecyclerView 滑动冲突
 *
 * @Author 35574
 * @Date 2020/9/29
 * @Description
 */
public class ScrollConflictHelper {
    private static final String TAG = ">>>ScrollConflictHelper";
    private static final boolean DEBUG = false;
    private boolean mAppBarLayoutTop = true;//appBarLayout是否滚动到顶部
    private boolean mRecyclerViewTop = true;//recyclerView是否滚动到顶部

    private RecyclerView recyclerView;
    private AppBarLayout appBarLayout;
    private SwipeRefreshLayout swipeRefreshLayout;

    public static ScrollConflictHelper init(RecyclerView recyclerView, AppBarLayout appBarLayout, SwipeRefreshLayout swipeRefreshLayout) {
        return new ScrollConflictHelper(recyclerView, appBarLayout, swipeRefreshLayout);
    }


    private ScrollConflictHelper(RecyclerView recyclerView, AppBarLayout appBarLayout, SwipeRefreshLayout swipeRefreshLayout) {
        this.recyclerView = recyclerView;
        this.appBarLayout = appBarLayout;
        this.swipeRefreshLayout = swipeRefreshLayout;
        addScrollListener();
    }


    /**
     * SwipeRefreshLayout和CoordinatorLayout嵌套滑动冲突
     */
    private void addScrollListener() {
        if (appBarLayout != null) {
            appBarLayout.addOnOffsetChangedListener((appBarLayout, i) -> {
                boolean appBarLayoutTop = i >= 0;
               /* if (DEBUG) {
                    LogUtils.d(TAG, "onOffsetChanged:" + i);
                }*/
                if (mAppBarLayoutTop != appBarLayoutTop) {
                    mAppBarLayoutTop = appBarLayoutTop;
                    setWipeRefreshLayoutEnable();
                }
            });
        }
        if (recyclerView != null) {
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    boolean recyclerViewTop = RecyclerViewUtils.isScrollTop(recyclerView);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        if (DEBUG) {
                            LogUtils.d(TAG, "onScrollStateChanged:");
                        }
                        if (mRecyclerViewTop != recyclerViewTop) {
                            mRecyclerViewTop = recyclerViewTop;
                            setWipeRefreshLayoutEnable();
                        }
                    }
                    super.onScrollStateChanged(recyclerView, newState);
                }

                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                }
            });

        }


    }

    /**
     * 是否启用下拉刷新
     */
    private void setWipeRefreshLayoutEnable() {
        if (swipeRefreshLayout != null) {
            if (DEBUG) {
                LogUtils.d(TAG, "mAppBarLayoutTop:" + mAppBarLayoutTop + ",mRecyclerViewTop=" + mRecyclerViewTop);
            }
            if (mAppBarLayoutTop && mRecyclerViewTop) {
                swipeRefreshLayout.setEnabled(true); //开启
            } else {
                swipeRefreshLayout.setEnabled(false); //关闭
            }
        }

    }


    /**
     * 设置AppBarLayout的子View的ScrollFlags
     *
     * @param childView 子View
     * @param flags     {@link AppBarLayout.LayoutParams#SCROLL_FLAG_ENTER_ALWAYS ...}
     */
    public static void setAppBarLayoutChildScrollFlags(View childView, int flags) {
        if (childView != null) {
            ViewGroup.LayoutParams layoutParams = childView.getLayoutParams();
            if (layoutParams instanceof AppBarLayout.LayoutParams) {
                ((AppBarLayout.LayoutParams) layoutParams).setScrollFlags(flags);
                childView.setLayoutParams(layoutParams);
            }
        }
    }

    /**
     * 储存AppBarLayout的子View的ScrollFlags
     */
    private int[] mAppBarChildrenFlags;
    private boolean mRollBack = true;//标记回复为原来的ScrollFlags

    /**
     * 初始化AppBarLayout的子View的ScrollFlags
     */
    private void initAppBarChildrenFlags() {
        if (appBarLayout != null && appBarLayout.getChildCount() > 0) {
            int childCount = appBarLayout.getChildCount();
            mAppBarChildrenFlags = new int[childCount];
            for (int i = 0; i < childCount; i++) {
                View childAt = appBarLayout.getChildAt(i);
                ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
                if (layoutParams instanceof AppBarLayout.LayoutParams) {
                    AppBarLayout.LayoutParams layoutParams1 = (AppBarLayout.LayoutParams) layoutParams;
                    mAppBarChildrenFlags[i] = layoutParams1.getScrollFlags();
                    layoutParams1.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                    childAt.setLayoutParams(layoutParams1);
                }
            }
        }
        mRollBack = false;
    }

    /**
     *
     */
    private void setAppBarChildrenFlags() {
        if (DEBUG) {
            LogUtils.d(TAG, "setAppBarChildrenFlags:mRollBack="+mRollBack);
        }
        if (appBarLayout != null && appBarLayout.getChildCount() > 0) {
            int childCount = appBarLayout.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = appBarLayout.getChildAt(i);
                ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
                if (layoutParams instanceof AppBarLayout.LayoutParams) {
                    AppBarLayout.LayoutParams layoutParams1 = (AppBarLayout.LayoutParams) layoutParams;
                    if (mRollBack) {
                        layoutParams1.setScrollFlags(mAppBarChildrenFlags[i]);
                    } else {
                        layoutParams1.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                    }
                    childAt.setLayoutParams(layoutParams1);
                }
            }
        }
    }


    public void registerAdapterDataObserver(BaseRecyclerViewAdapter<?> adapter) {
        initAppBarChildrenFlags();
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (DEBUG) {
                    LogUtils.d(TAG, "onChanged:");
                }
                int dataSize = adapter.getDataSize();
                if (dataSize > 0 && !mRollBack) {
                    mRollBack = true;
                    setAppBarChildrenFlags();
                } else if (dataSize == 0 && mRollBack) {
                    mRollBack = false;
                    setAppBarChildrenFlags();
                }

            }
        });

    }

}
